# Code for Table Q1: Race and Pretreatment Turnout

rm(list = ls())

## ---------------------------------------
## Load Packages 
## ---------------------------------------

library('AER')
library('ivpack')
library('dplyr')
library('data.table')
library('stargazer')

## ---------------------------------------
## Load Data and Functions
## ---------------------------------------
load("../data0812.RData")

directory <- "../functions/"
functions <- list.files(directory)  
loadfunctions <- sapply(functions, FUN = function(x)source(paste0(directory, x)))

## ---------------------------------------
#  Select Last Case Before Election
## ---------------------------------------
data0812 <- lastCase(data0812)

data0812$Black <- ifelse(data0812$race == "Black", 1, 0)

## ---------------------------------------
#  OLS
## ---------------------------------------
m1 <- lm(vote2008 ~ Black, data=data0812[(race %in% c("Black", "White")) & noteli08 == 0])
summary(m1)


m2 <- lm(vote2008 ~ Black + Female + age_2012  + totOGS2, data=data0812[(race %in% c("Black", "White")) & noteli08 == 0])
summary(m2)

cat("\nPrinting Table Q1: Race and Pretreatment Turnout...\n")
print(stargazer(m1, m2, covariate.labels = c("Black", "Male", "Age", "Offense Gravity Score"),
          dep.var.labels.include = FALSE,
          type = "text", 
          column.labels = c("2008 Turnout (Raw)", "2008 Turnout (Re-weighted)"),
          omit.table.layout = "n",
          notes = c("Sample includes only  Black and White defendants who were 18 or older at the time of the 2008 election.")))
